package com.flca.frw.util
{
	import com.flca.frw.config.MasterItem;
	
	import mx.controls.Alert;
	import mx.controls.ComboBox;
	import mx.core.UIComponent;
	
	public class EnumHelper
	{
		public static function updateToCombobox(targetCombobox:UIComponent,
			currentValue:String, values:Array, aLabelFunction:Function = null) : void 
		{
			if (targetCombobox != null && 
				targetCombobox is ComboBox && values != null) 
			{
				var cb:ComboBox = targetCombobox as ComboBox;

				if (aLabelFunction != null) {
					cb.labelFunction = aLabelFunction;
				}
				cb.dataProvider = values;
	
				cb.selectedIndex = getIndexByEnum(values, currentValue);			
			} else {
				Alert.show("precondition failed for updateToCombobox");
			}		
		}
		
		/**
		 * 
		 */ 
		public static function updateFromCombobox(srcCombobox:UIComponent,
			setter:Function, values:Array) : void 
		{
			if (setter != null) {
				if (srcCombobox != null && srcCombobox is ComboBox) {
					var cb:ComboBox = srcCombobox as ComboBox;
					if (cb.value != null) {
						setter(cb.value.toString());
					} else {
						setter(null);
					}
				} else {
					Alert.show("precondition failed for updateFromCombobox");
				}		
			}
		}
		

		public static function getIndexByEnum(aValues: Array, aValue: String) : int
		{
			if (aValue == null) {
				return -1;
			}
			
			return getIndexByString(aValues, aValue);
		}
		
		public static function getIndexByString(aValues: Array, aValue: String) : int
		{
			if (aValue == null) {
				return -1;
			}
			
			for (var i:int = 0; i < aValues.length; i++) {
				var obj: Object = aValues[i];
				if (obj is String) {
					if ((obj as String) == aValue) {
						return i;
					} 
				} else if (obj is MasterItem) {
					if ((obj as MasterItem).data == aValue) {
						return i;
					}
				} 
			}	
			return -1;
		}
		
		
		public static function compareEnumWithCombo(aEnumValue:String, aCombobox:ComboBox) : Boolean 
		{
			if (aCombobox != null && aCombobox.value != null && aEnumValue != null) {
				return aCombobox.value.toString() == aEnumValue;
			}	else {
				return false;
			}
		}

		public static function updateStringToCombobox(targetCombobox:UIComponent,
			currentValue:String, values:Array, aLabelFunction:Function = null) : void 
		{
			if (targetCombobox != null && 
				targetCombobox is ComboBox && values != null) 
			{
				var cb:ComboBox = targetCombobox as ComboBox;

				if (aLabelFunction != null) {
					cb.labelFunction = aLabelFunction;
				}
				cb.dataProvider = values;
	
				cb.selectedIndex = getIndexByString(values, currentValue);			
			} else {
				Alert.show("precondition failed for updateToCombobox");
			}		
		}

		public static function updateStringFromCombobox(srcCombobox:UIComponent,
			setter:Function, values:Array) : void 
		{
			if (srcCombobox != null && srcCombobox is ComboBox) 
			{
				var cb:ComboBox = srcCombobox as ComboBox;
				if (cb.value != null) {
					setter(cb.value.toString());
				} else {
					//currentValue = null;
				}
			} else {
				Alert.show("precondition failed for updateFromCombobox");
			}		
		}

		public static function setEnumValue(srcCombobox:UIComponent, aEnum:String) : void
		{
			if (srcCombobox != null && srcCombobox is ComboBox) 
			{
				var cb:ComboBox = srcCombobox as ComboBox;
				if (cb.value != null) {
					aEnum = cb.value.toString();
				} 
			} else {
				Alert.show("precondition failed for updateFromCombobox");
			}		
		}
		
		public static function setComboxValue(aEnumValue:String, enumValues: Array, targetCombobox:UIComponent) : void
		{
			if (targetCombobox != null && targetCombobox is ComboBox) 
			{
				var cb:ComboBox = targetCombobox as ComboBox;
				var idx:int = getIndexByEnum(enumValues, aEnumValue);
				cb.selectedIndex = idx;
			} else {
				Alert.show("precondition failed for setComboxValue");
			}		
		}
		
		/**
		 * Insert a default value like "ANY" as the first item on the copy of the given array
		 */
		public static function insertDefaultValue(arr:Array, defValue:String) : Array
		{
			var r : Array = new Array();
			
			r.push(defValue);
			
			for each (var o:Object in arr) {
				r.push(o);
			}
		
			return r;		
		}

	}
}